package com.time.company.viewmodel;

import android.support.annotation.NonNull;
import android.text.TextUtils;


import com.time.company.R;
import com.time.company.app.MyApplication;
import com.time.company.db.model.DynamicsModel;
import com.time.company.db.model.PicModel;
import com.time.company.servermodel.dynamics.Pic;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bing on 2017/10/26.
 * E-mail is tx16bing@163.com
 */

public class DynamicsViewModel  implements Comparable<DynamicsViewModel>{

    private String userId;
    private String showName;
    private String userHead;

    private String createTime;
    private String showTime;

    private String dynamicsId;
    private String content;
    private String location;
    private String commentNum;
    private String likeNum;
    private boolean isLike;
    private String shareUrl;

    private List<PicModel> pics;

    public DynamicsViewModel() {
        // Nothing to do
    }

    public DynamicsViewModel(MyApplication application, DynamicsModel model) {
        this.dynamicsId = model.getDynamicsId();
        this.userId = model.getUserId();
        this.showName = model.getUserName();
        this.userHead = model.getLogoUrl();

        this.content = model.getContent();
        this.commentNum = model.getCommentNum();
        this.likeNum = model.getLikeNum();
        this.location = model.getLocation();

        if (TextUtils.equals(application.getString(R.string.zero), model.getLikeState())){
            this.isLike = true;
        }else {
            this.isLike = false;
        }
        this.shareUrl = model.getShareUrl();

        this.createTime = model.getCreateAt();
        if (!TextUtils.isEmpty(this.createTime) && this.createTime.length() >= 19){
            String[] split = this.createTime.split(" ");
            String time = split[1].substring(0, 5);
            this.showTime = new StringBuilder().append(split[0]).append(" ").append(time).toString();
        }else {
            this.showTime = model.getCreateAt();
        }

        if (model.getPicList() == null) {
            this.pics = new ArrayList<>();
        } else {
            this.pics = new ArrayList<>();
            for (Pic pic : model.getPicList()) {
                this.pics.add(new PicModel(pic));
            }
        }

    }

    public DynamicsViewModel(MyApplication application, DynamicsModel model, List<PicModel> picModels) {
        this.dynamicsId = model.getDynamicsId();
        this.userId = model.getUserId();
        this.showName = model.getUserName();
        this.userHead = model.getLogoUrl();

        this.content = model.getContent();
        this.likeNum = model.getLikeNum();
        this.location = model.getLocation();

        if (TextUtils.equals(application.getString(R.string.zero), model.getLikeState())){
            this.isLike = true;
        }else {
            this.isLike = false;
        }

        this.createTime = model.getCreateAt();
        if (!TextUtils.isEmpty(this.createTime) && this.createTime.length() >= 19){
            String[] split = this.createTime.split(" ");
            String date = split[1].replace("-", "/");
            String time = split[1].substring(0, 6);
            this.showTime = new StringBuilder().append(date).append(" ").append(time).toString();
        }else {
            this.showTime = model.getCreateAt();
        }

        if (picModels == null) {
            this.pics = new ArrayList<>();
        } else {
            this.pics = picModels;
        }
    }

    @Override
    public int compareTo(@NonNull DynamicsViewModel other) {
        if (TextUtils.isEmpty(this.createTime)) {
            return -1;
        } else {
            if (!TextUtils.isEmpty(other.getCreateTime())) {
                return other.getCreateTime().compareTo(this.createTime);
            } else {
                return -1;
            }
        }
    }

    public String getDynamicsId() {
        return dynamicsId;
    }

    public void setDynamicsId(String dynamicsId) {
        this.dynamicsId = dynamicsId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getShowName() {
        return showName;
    }

    public void setShowName(String showName) {
        this.showName = showName;
    }

    public String getUserHead() {
        return userHead;
    }

    public void setUserHead(String userHead) {
        this.userHead = userHead;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getShowTime() {
        return showTime;
    }

    public void setShowTime(String showTime) {
        this.showTime = showTime;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getLikeNum() {
        return likeNum;
    }

    public void setLikeNum(String likeNum) {
        this.likeNum = likeNum;
    }

    public boolean isLike() {
        return isLike;
    }

    public void setLike(boolean like) {
        isLike = like;
    }

    public String getShareUrl() {
        return shareUrl;
    }

    public void setShareUrl(String shareUrl) {
        this.shareUrl = shareUrl;
    }

    public List<PicModel> getPics() {
        return pics;
    }

    public void setPics(List<PicModel> pics) {
        this.pics = pics;
    }

    public String getCommentNum() {
        return commentNum;
    }

    public void setCommentNum(String commentNum) {
        this.commentNum = commentNum;
    }
}
